package ch10.Date;

import ch10.gongju.JdbcUtils;
import ch10.UserBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//添加用户的信息
public class UserDao {
    public  UserBean findByUserCode(String userCode,String userPassword) throws SQLException, ClassNotFoundException {
        Connection connection =null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT *FROM smbms_user WHERE userCode=? and userPassword=?";
        try {
            connection =JdbcUtils.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1,userCode);
            ps.setString(2,userPassword);
            rs = ps.executeQuery();
            while (rs.next()){
                UserBean userBean = new UserBean();
                userBean.setId(rs.getInt("id"));
                userBean.setUserCode(rs.getString("userCode"));
                userBean.setUserName(rs.getString("userName"));
                userBean.setUserPassword(rs.getString("userPassword"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setBirthday(rs.getDate("birthday"));
                userBean.setPhone(rs.getString("phone"));
                userBean.setAddress(rs.getString("address"));
                userBean.setUserRole(rs.getInt("userRole"));
//                userBeans.add(userBean);
                return userBean;
            }
            //查不到数据
            return null;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(connection,ps,rs);//close会创造到jdbcutils.java
        }


    }
//    public Integer addUser(UserBean userBean){
//        Connection connection = null;
//        PreparedStatement ps = null;
//        String sql = "SELECT *FROM smbms_user WHERE id=?";
//        connection = JdbcUtils.getConnection()
//    }
}
